<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态 CRUD 系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <link rel="stylesheet" href="/static/styles.css">
</head>
<body>
    <div class="container mt-5">
        <!-- 数据库连接配置 -->
        <div class="card mb-4" style="display: none;" id="dbConfigCard">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">数据库连接配置</h5>
                <button class="btn btn-sm btn-primary" onclick="toggleDBConfigForm()">显示/隐藏</button>
            </div>
            <div class="card-body" id="dbConfigForm">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">主机</label>
                        <input type="text" id="dbHost" class="form-control" placeholder="localhost 或 IP 地址">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">端口</label>
                        <input type="text" id="dbPort" class="form-control" placeholder="3306">
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">用户名</label>
                        <input type="text" id="dbUser" class="form-control" placeholder="数据库用户名">
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">密码</label>
                        <input type="password" id="dbPassword" class="form-control" placeholder="数据库密码">
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">数据库名</label>
                        <input type="text" id="dbName" class="form-control" placeholder="数据库名">
                    </div>
                    <div class="col-md-6 mb-3 d-flex align-items-end">
                        <button class="btn btn-success w-100" onclick="updateDBConfig()">连接数据库</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 数据库连接状态 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <button class="btn btn-outline-primary" onclick="showDBConfig()">数据库配置</button>
            </div>
            <div id="dbConnectionInfo" class="text-muted">
                <small>使用.env文件中的配置连接到数据库</small>
            </div>
        </div>

        <!-- 数据库和表选择 -->
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <strong>数据库选择</strong>
                    </div>
                    <div class="card-body">
                        <div class="input-group">
                            <span class="input-group-text">数据库名</span>
                            <select id="dbSelect" class="form-select" onchange="onDatabaseChange()">
                                <option value="">加载中...</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <strong>数据表选择</strong>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <div class="input-group">
                                <span class="input-group-text">搜索表</span>
                                <input type="text" id="tableSearchInput" class="form-control" placeholder="输入关键字搜索表..." oninput="filterTables()">
                                <button class="btn btn-outline-secondary" type="button" onclick="clearTableSearch()">
                                    <i class="bi bi-x-lg"></i>
                                </button>
                            </div>
                            <div id="tableSearchResults" class="mt-1 small text-muted"></div>
                        </div>
                        <div class="input-group mb-3">
                            <span class="input-group-text">选择数据表</span>
                            <select id="tableSelect" class="form-select" onchange="onTableChange()">
                                <option value="">请选择数据表...</option>
                            </select>
                            <button class="btn btn-success" onclick="showCreateTableModal()">
                                <i class="bi bi-plus-circle"></i> 创建表
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 表结构管理 -->
        <h2>表结构管理</h2>
        <div class="card mb-4">
            <div class="card-header bg-light">
                <strong>添加新列</strong>
            </div>
            <div class="card-body">
                <div class="row mb-3">
                    <div class="col-md-6">
                        <label class="form-label">列名</label>
                        <input type="text" id="columnName" class="form-control" placeholder="列名">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">数据类型</label>
                        <select id="columnType" class="form-select">
                            <!-- 字符串类型 -->
                            <optgroup label="字符串类型">
                                <option value="TEXT">文本</option>
                                <option value="VARCHAR">可变长文本</option>
                                <option value="CHAR">定长文本</option>
                                <option value="LONGTEXT">长文本</option>
                                <option value="MEDIUMTEXT">中等文本</option>
                                <option value="TINYTEXT">短文本</option>
                            </optgroup>
                            <!-- 数值类型 -->
                            <optgroup label="数值类型">
                                <option value="INTEGER">整数</option>
                                <option value="BIGINT">大整数</option>
                                <option value="SMALLINT">小整数</option>
                                <option value="TINYINT">微整数</option>
                                <option value="FLOAT">单精度浮点</option>
                                <option value="DOUBLE">双精度浮点</option>
                                <option value="DECIMAL">定点数</option>
                            </optgroup>
                            <!-- 日期和时间类型 -->
                            <optgroup label="日期和时间类型">
                                <option value="DATE">日期</option>
                                <option value="TIME">时间</option>
                                <option value="DATETIME">日期时间</option>
                                <option value="TIMESTAMP">时间戳</option>
                                <option value="YEAR">年份</option>
                            </optgroup>
                            <!-- 其他类型 -->
                            <optgroup label="其他类型">
                                <option value="BOOLEAN">布尔值</option>
                                <option value="JSON">JSON</option>
                            </optgroup>
                        </select>
                    </div>
                </div>
                
                <div class="row mb-3">
                    <div class="col-md-12">
                        <label class="form-label">注释</label>
                        <input type="text" id="columnDescription" class="form-control" placeholder="列的注释">
                        <div class="form-text">描述该列的用途，默认为列名</div>
                    </div>
                </div>
                
                <div class="row mb-3">
                    <div class="col-md-6">
                        <div class="mb-3">
                            <label class="form-label">长度/精度</label>
                            <input type="number" class="form-control" id="columnLength" placeholder="长度/精度 (可选)">
                            <div class="form-text">例如：VARCHAR(255) 中的 255，DECIMAL(10,2) 中的 10</div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">小数位数</label>
                            <input type="number" class="form-control" id="columnScale" placeholder="小数位数 (可选)">
                            <div class="form-text">仅适用于 DECIMAL 类型，例如：DECIMAL(10,2) 中的 2</div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">默认值</label>
                            <input type="text" class="form-control" id="columnDefaultValue" placeholder="默认值 (可选)">
                            <div class="form-text">留空表示没有默认值</div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" id="columnNotNull">
                            <label class="form-check-label" for="columnNotNull">非空 (NOT NULL)</label>
                        </div>
                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" id="columnUnique">
                            <label class="form-check-label" for="columnUnique">唯一 (UNIQUE)</label>
                        </div>
                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" id="columnUnsigned">
                            <label class="form-check-label" for="columnUnsigned">无符号 (UNSIGNED)</label>
                            <div class="form-text">仅适用于数值类型</div>
                        </div>
                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" id="columnAutoIncrement">
                            <label class="form-check-label" for="columnAutoIncrement">自动递增 (AUTO_INCREMENT)</label>
                            <div class="form-text">仅适用于整数类型，通常用于主键</div>
                        </div>
                        <div class="mb-3 form-check">
                            <input type="checkbox" class="form-check-input" id="columnPrimaryKey">
                            <label class="form-check-label" for="columnPrimaryKey">主键 (PRIMARY KEY)</label>
                            <div class="form-text">一个表只能有一个主键</div>
                        </div>
                    </div>
                </div>
                
                <div class="text-end">
                    <button class="btn btn-success" onclick="addColumn()">添加列</button>
                </div>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-bordered" id="structureTable">
                <thead>
                    <tr>
                        <th>列名</th>
                        <th>类型</th>
                        <th>属性</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="structureBody"></tbody>
            </table>
        </div>

        <!-- 数据管理 -->
        <h2 class="mt-5">数据管理</h2>
        <div class="row mb-3">
            <div class="col-md-6">
                <button class="btn btn-success mb-3" onclick="showAddForm()">添加数据</button>
            </div>
            <div class="col-md-6">
                <!-- 快速筛选区 -->
                <div class="input-group mb-3">
                    <input type="text" id="searchId" class="form-control" placeholder="输入ID查询">
                    <button class="btn btn-primary" onclick="searchById()">查询</button>
                    <button class="btn btn-secondary" onclick="loadData()">显示全部</button>
                </div>
                <div class="input-group mb-3">
                    <input type="text" id="searchName" class="form-control" placeholder="输入name查询">
                    <button class="btn btn-primary" onclick="searchByName()">查询</button>
                </div>
                
                <!-- 高级筛选开关 -->
                <div class="text-end mb-2">
                    <button class="btn btn-outline-info btn-sm" onclick="toggleAdvancedFilter()">
                        <i class="bi bi-funnel"></i> 高级筛选
                    </button>
                </div>
                
                <!-- 高级筛选区 -->
                <div id="advancedFilterArea" class="card mb-3" style="display: none;">
                    <div class="card-header bg-light">
                        <div class="d-flex justify-content-between align-items-center">
                            <strong>多条件筛选</strong>
                            <button type="button" class="btn-close" onclick="toggleAdvancedFilter()"></button>
                        </div>
                    </div>
                    <div class="card-body">
                        <div id="filterConditions">
                            <!-- 筛选条件将在这里动态添加 -->
                            <div class="filter-condition mb-2">
                                <div class="row g-2">
                                    <div class="col-5">
                                        <select class="form-select filter-field">
                                            <option value="">选择字段</option>
                                            <!-- 字段选项将动态填充 -->
                                        </select>
                                    </div>
                                    <div class="col-3">
                                        <select class="form-select filter-operator">
                                            <option value="eq">等于</option>
                                            <option value="ne">不等于</option>
                                            <option value="gt">大于</option>
                                            <option value="lt">小于</option>
                                            <option value="like">包含</option>
                                        </select>
                                    </div>
                                    <div class="col-4">
                                        <input type="text" class="form-control filter-value" placeholder="值">
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="d-flex justify-content-between mt-3">
                            <button class="btn btn-outline-secondary btn-sm" onclick="addFilterCondition()">
                                <i class="bi bi-plus-circle"></i> 添加条件
                            </button>
                            <div>
                                <button class="btn btn-primary btn-sm" onclick="applyMultiFilter()">应用筛选</button>
                                <button class="btn btn-outline-secondary btn-sm" onclick="clearFilters()">清除</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="table-responsive">
            <table class="table table-bordered" id="dataTable">
                <thead id="dataHeader"></thead>
                <tbody id="dataBody"></tbody>
            </table>
        </div>

        <!-- 添加/编辑数据的模态框 -->
        <div class="modal fade" id="dataModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="modalTitle">添加数据</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body" id="modalBody"></div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" id="saveDataBtn">保存</button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 创建表的模态框 -->
        <div class="modal fade" id="createTableModal" tabindex="-1">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">创建新表</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">表名 <small class="text-muted">(使用SQL时可选)</small></label>
                            <input type="text" class="form-control" id="newTableName" placeholder="请输入表名">
                            <div class="form-text">如果提供SQL语句，系统将尝试从SQL中提取表名</div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">SQL语句 <small class="text-muted">(可选)</small></label>
                            <textarea class="form-control" id="createTableSQL" rows="5" placeholder="CREATE TABLE 语句，留空则创建只有ID列的空表"></textarea>
                            <div class="form-text">如果提供SQL，必须是CREATE TABLE语句。留空则创建只有ID列的空表。</div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" id="createTableBtn">创建表</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="/static/script.js"></script>
</body>
</html> 